package terrain.impl.first;

import interfaces.WorldNodeInterface;

import java.awt.Color;
import java.awt.Graphics;

import objects.ImageObject;
import extra.Position;


public class WorldNodeFirst implements WorldNodeInterface{
	
	Position position;
	boolean isFree;
	
	public WorldNodeFirst(){
		isFree=true;
		position= new Position();
	}

	@Override
	public void calculateCollision(ImageObject collisionImage) {
		Position posCollitionImage = collisionImage.getPosition();
		
		int coutCollision=0;
		
		int x= (int) (position.getX()+ConstantsFirst.NODE_WIDTH*0.25);
		int y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.25);
		
		//System.out.println("XC: "+x+" Yc: "+y);
		
		
		if(collisionImage.isInside(x, y)){
			//System.out.println("Enonre collision12 "+collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY())));
			//System.out.println("otra : "+ConstantsFirst.COLLISIO_COLOR);
			if(collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY()))==ConstantsFirst.COLLISIO_COLOR){
				
				coutCollision++;
			}
		}
		
		x= (int) (position.getX()+ConstantsFirst.NODE_WIDTH*0.75);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.75);
		if(collisionImage.isInside(x, y)){
			if(collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY()))==ConstantsFirst.COLLISIO_COLOR){
				coutCollision++;
			}
		}
		
		
		x= (int) (position.getX()+ConstantsFirst.NODE_WIDTH*0.25);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.75);
		if(collisionImage.isInside(x, y)){
			if(collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY()))==ConstantsFirst.COLLISIO_COLOR){
				coutCollision++;
			}
		}
		
		x= (int) (position.getX()+ConstantsFirst.NODE_WIDTH*0.75);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.25);
		if(collisionImage.isInside(x, y)){
			if(collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY()))==ConstantsFirst.COLLISIO_COLOR){
				coutCollision++;
			}
		}
		
		x= (int) (position.getX()+ConstantsFirst.NODE_WIDTH*0.5);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.5);
		//System.out.println("Num Colisiones0: "+coutCollision);
		if(collisionImage.isInside(x, y)){
			//System.out.println("Num Colisiones: "+coutCollision+" RGB Black "+Color.white.getRGB() );
			//System.out.println("RGB IMG "+ collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY())));
			if(collisionImage.getCollisionShape().getRGB( (int) (x-posCollitionImage.getX()), (int)(y-posCollitionImage.getY()))==ConstantsFirst.COLLISIO_COLOR){
				coutCollision++;
			}
		}
		
		if(coutCollision>=2){
			//System.out.println("Enonre collision");
			isFree=false;
		}
		
		
	}

	@Override
	public Position getPosition() {
		
		return position;
	}

	@Override
	public boolean isFree() {

		return isFree;
	}

	@Override
	public void setPosition(Position position) {

		this.position=position;
		
	}

	@Override
	public void setIsFree(boolean isFree) {
		
		this.isFree=isFree;
		
	}

	@Override
	public void paint(Graphics g) {
		
		if(isFree){
			g.setColor(Color.BLUE);
		}
		else{
			g.setColor(Color.RED);
		}
		g.fillRect((int)position.getX(), (int)position.getY(), ConstantsFirst.NODE_WIDTH, ConstantsFirst.NODE_HEIGHT);
		
		g.setColor(Color.BLACK);
		g.drawRect((int)position.getX(), (int)position.getY(), ConstantsFirst.NODE_WIDTH, ConstantsFirst.NODE_HEIGHT);
		
		
		int x= (int) (position.getX()+ConstantsFirst.NODE_HEIGHT*0.25);
		int y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.25);
		g.drawLine(x, y, x, y);
		
		x= (int) (position.getX()+ConstantsFirst.NODE_HEIGHT*0.75);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.75);
		g.drawLine(x, y, x, y);
		
		x= (int) (position.getX()+ConstantsFirst.NODE_HEIGHT*0.25);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.75);
		g.drawLine(x, y, x, y);
		
		x= (int) (position.getX()+ConstantsFirst.NODE_HEIGHT*0.75);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.25);
		g.drawLine(x, y, x, y);
		
		x= (int) (position.getX()+ConstantsFirst.NODE_HEIGHT*0.5);
		y= (int) (position.getY()+ConstantsFirst.NODE_HEIGHT*0.5);
		g.drawLine(x, y, x, y);
		
		
	}

	@Override
	public Position getMiddlePosition() {

		return new Position(position.getX()+ConstantsFirst.NODE_WIDTH/2,position.getY()+ConstantsFirst.NODE_HEIGHT/2);
	}
	
	

}
